#include"myFILE.h"


MYFILE* fopen(const char*path,const char*option)
{
  MYFILE* tmp=(MYFILE*)malloc(sizeof(MYFILE));
  tmp->pos=0;
  tmp->mode=FULL_BUFF;
  if(option[0]=='w')
  {
    tmp->fileno=open(path,O_WRONLY|O_CREAT|O_TRUNC,0666);
  }
  else if(option[0]=='a')
  {
    tmp->fileno=open(path,O_WRONLY|O_CREAT|O_APPEND|0666);
  }
  else if(option[0]=='r')
  {
    tmp->fileno=open(path,O_RDONLY);
  }
  else return NULL;
  return tmp;
}


int mywrite(const char*str,int size,int n,MYFILE*fp)
{
  while(n--)
  {  
    if(fp->mode==FULL_BUFF)
    {
      strncpy(fp->buffer+fp->pos,str,size);
      fp->pos=strlen(fp->buffer);
    }
    else
    {
         write(fp->fileno,str,size);
    }
  }
return size;
}
int fclose(MYFILE*fp)
{
  if(fp->pos!=0)write(fp->fileno,fp->buffer,fp->pos);
  close(fp->fileno);
  free(fp);
  fp=NULL;
  return 0;
}
